package com.hsck.order.kds.base.isnoam.view.keyboard

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.GridView
import android.widget.RelativeLayout
import com.hsck.order.kds.R

/**
 * @author isnoam
 * @package_name VirtualKeyboardView
 * @description：
 */


class VirtualKeyboardView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
    RelativeLayout(context, attrs) {
    private var context: Context

    private val gridView: GridView
    private val valueList: ArrayList<Map<String, String>>

    init {
        this.context = context
        val view = inflate(context, R.layout.layout_virtual_keyboard, null)
        valueList = ArrayList()
        gridView = view.findViewById<View>(R.id.gv_keybord) as GridView
        initValueList()
        setupView()
        addView(view) //必须要，不然不显示控件
    }

    fun setWeightData() {
        valueList.clear()
        for (i in 1..12) {
            val map: MutableMap<String, String> = HashMap()
            if (i < 10) {
                map["name"] = i.toString()
            } else if (i == 10) {
                map["name"] = "."
            } else if (i == 11) {
                map["name"] = 0.toString()
            } else /*if (i == 12)*/ {
                map["name"] = ""
            }
            valueList.add(map)
        }
        setupView()
    }

    fun getValueList(): ArrayList<Map<String, String>> {
        return valueList
    }

    fun initValueList() {
        valueList.clear()
        // 初始化按钮上应该显示的数字
        for (i in 1..12) {
            val map: MutableMap<String, String> = HashMap()
            if (i < 10) {
                map["name"] = i.toString()
            } else if (i == 10) {
                map["name"] = "清空"
            } else if (i == 11) {
                map["name"] = 0.toString()
            } else /*if (i == 12)*/ {
                map["name"] = ""
            }
            valueList.add(map)
        }
    }

    fun getGridView(): GridView {
        return gridView
    }

    private fun setupView() {
        val keyBoardAdapter = KeyBoardAdapter(context, valueList)
        gridView.setAdapter(keyBoardAdapter)
    }
}